import interceptor from 'rest/interceptor';
import when from 'when';
import { message } from 'antd';

export default interceptor({
  init: function (config) {
    config.code = config.code || 400;
    return config;
  },
  response: function (response, config) {

    console.log('error code response.................');
    if (response.status && response.status.code === 401) {
      return when.reject(response);
    }
    if (response.status && response.status.code === 417) {
        message.error('用户名密码不正确');
        return when.reject(response);
    }
    if (response.status && response.status.code >= config.code) {
      if (response.entity) {
        let entity = response.entity;
        let msg = entity.message;
        if (msg === 'error.validation') {
          message.error(entity.fieldErrors && entity.fieldErrors[0].msg || "信息填写不全");
        } else if (msg === 'error.accessDenied') {
          message.error("无权访问");
        } else if (msg === 'error.concurrencyFailure' || msg === 'error.methodNotSupported' || msg === 'error.internalServerError') {
          message.error("服务器繁忙,请稍后再试");
        }else if(response.status.code === 403){
          //登录验证错误出现403防止出现报错

        } else {
          message.error(msg || "服务器繁忙,请稍后再试");
        }
      }
      return when.reject(response);
    }
    return response;
  }
});
